<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*数字资源-资源每天访问次数表*/

class ResourceTotalAccessNumUserDay extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_total_access_num_user_day';

    /**
     * 添加用户阅读时间
     * @param resource_guid
     * @param resource_business_id
     * @param big_type_id
     * @param browse_num  浏览量
     */
    public function addAccessNumUserDay($user_id, $resource_guid, $resource_business_id, $big_type_id = null, $date = null, $browse_num = 1)
    {
        $date = $date ? $date : date('Y-m-d');
        $res = $this->where('user_id', $user_id)->where('resource_guid', $resource_guid)->where('resource_business_id', $resource_business_id)->where('big_type_id', $big_type_id)->where('date', $date)->first();
        if (empty($res)) {
            $res = $this;
            $res->number = $browse_num;
            $res->big_type_id = $big_type_id;
        } else {
            $res->number = $res->number + $browse_num; //访问次数
        }
        $res->user_id = $user_id;
        $res->resource_guid = $resource_guid;
        $res->resource_business_id = $resource_business_id;
        $res->date = $date;
        $res->save();
        return true;
    }
}
